import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards } from '@nestjs/common';
import { FieldTypeService } from './field-type.service';
import { FieldType } from './field-type.entity';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { RequireLevel } from '../auth/roles.decorator';

@Controller('field-type')
@UseGuards(JwtAuthGuard, RolesGuard)
export class FieldTypeController {
  constructor(private readonly fieldTypeService: FieldTypeService) {}

  @Get()
  async findAll(): Promise<FieldType[]> {
    return this.fieldTypeService.findAll();
  }

  @Get(':id')
  async findOne(@Param('id') id: number): Promise<FieldType> {
    return this.fieldTypeService.findOne(id);
  }

  @Post()
  @RequireLevel(1)
  async create(@Body() fieldTypeData: Partial<FieldType>): Promise<FieldType> {
    return this.fieldTypeService.create(fieldTypeData);
  }

  @Put(':id')
  @RequireLevel(1)
  async update(@Param('id') id: number, @Body() fieldTypeData: Partial<FieldType>): Promise<FieldType> {
    return this.fieldTypeService.update(id, fieldTypeData);
  }

  @Delete(':id')
  @RequireLevel(1)
  async remove(@Param('id') id: number): Promise<void> {
    return this.fieldTypeService.remove(id);
  }

  @Post('initialize')
  @RequireLevel(1)
  async initializeDefaultFields(): Promise<{ message: string }> {
    await this.fieldTypeService.initializeDefaultFields();
    return { message: '默认字段类型初始化成功' };
  }
}